home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / ici / ici.cpi / alloc.h < prev    next >
C/C++ Source or Header  |  1994-10-27  |  5KB  |  153 lines

  1. #ifndef    ICI_ALLOC_H
  2. #define    ICI_ALLOC_H
  3.  
  4. /*
  5.  * If WHOALLOC is defined to be non-zero (i.e. 1) every allocation, records
  6.  * the C source file and line number it was made on and how much was allocated.
  7.  * It is them possible to print a summary, after running for a while, of what
  8.  * is still allocated. Mainly for finding leaks, but it can also be
  9.  * used for other debugging. Changing this requires total recompilation.
  10.  */
  11. #define    WHOALLOC    0    /* Define to 1 to track who is using memory. */
  12.  
  13. /*
  14.  * Most allocated things will fit within 24 bytes, many of the rest within
  15.  * about 50.  We keep our own lists of things of these sizes, and use the
  16.  * native system's mechanism for the remainder.  Each items on our lists
  17.  * is also allocated directly from the system.  Clearly some time and space
  18.  * advantage would be gained from allocating in larger chunks and then
  19.  * splitting up ourselves, but there are disadvantages too.
  20.  *
  21.  * Raw malloc's will end up being done of the sizes given here plus
  22.  * sizeof(int) plus futher alignment requirements.  We allow these to be
  23.  * set from the configuration file so they can be fine tuned for a
  24.  * particular malloc. (eg. 64 may be a non-optimal size form some mallocs).
  25.  */
  26.  
  27. #ifndef    LIST1Z
  28. #define    LIST1Z        24
  29. #endif
  30. #define    FEW_OBJS    (LIST1Z / 4)
  31.  
  32. #ifndef    LIST2Z
  33. #define    LIST2Z        56
  34. #endif
  35. #define    FEW_MORE_OBJS    (LIST2Z / 4)
  36.  
  37. /*
  38.  * To be sure that the area returned by ici_alloc is properly aligned
  39.  * we must add to the pointer originally returned by malloc a size
  40.  * which will maintain the worst case alignment requirement.
  41.  * Even though we only need one byte of space for our header.
  42.  *
  43.  * The following structure is used as an overlay to make sure we add
  44.  * the right amount to start user data at a good alignment position.
  45.  */
  46. typedef struct
  47. {
  48. #if    !WHOALLOC
  49.     int        ga_list;    /* Which fast free list, 0 if none. */
  50. #else
  51.     short    ga_list;
  52.     short    ga_who;
  53.     int        ga_size;
  54. #endif
  55.     union
  56.     {
  57.     char    gau_data[1];    /* The first byte of user data when not free.*/
  58.     char    *gau_next;    /* Next free block when on fast free list. */
  59.     double    gau_align;    /* Only to force alignment. */
  60.     }
  61.     gau;
  62. }
  63.     gahead_t;
  64.  
  65. /*
  66.  * The number of bytes between the prefix block header and the start
  67.  * of the user data. This is the overhead.
  68.  */
  69. #define    GALLOC_HEADZ    (((gahead_t *)NULL)->gau.gau_data \
  70.             -(char *)&((gahead_t *)NULL)->ga_list)
  71. /*
  72.  * Return a pointer to the header, given the user pointer.
  73.  */
  74. #define    gahead(p)    ((gahead_t *)((char *)p - GALLOC_HEADZ))
  75. /*
  76.  * Return the user pointer given a pointer to the header.
  77.  */
  78. #define    gadata(p)    (((gahead_t *)p)->gau.gau_data)
  79. /*
  80.  * Given a size, which, if any, fast free list should it be on?
  81.  * Note that this will be a constant expression if z is a constant.
  82.  */
  83. #define    galist(z)    ((z) <= LIST1Z ? 1 : (z) <= LIST2Z ? 2 : 0)
  84.  
  85. /*
  86.  * Allocate memory as if by ici_alloc(). But if there is currently a block
  87.  * of suitable size on one of the fast free lists, just use that. If there
  88.  * isn't resort to the real ici_alloc() function.
  89.  *
  90.  * Every block allocated by ici_alloc() is prefixed by a header which contains
  91.  * an int. This int is 0 in general, but if the block is of an appropriate
  92.  * size for one of the fast free lists, it is a small integer index of that
  93.  * free list.
  94.  */
  95. #define    zalloc(z) \
  96.         (ici_fflist[galist(z)] != NULL \
  97.         ? ici_new_mem += (z), ici_fflist[galist(z)] \
  98.             = *(char **)(ici_fftmp = ici_fflist[galist(z)]), ici_fftmp \
  99.         : ici_alloc(z))
  100.  
  101. /*
  102.  * Free memory allocated with ici_alloc/zalloc. If it belongs on one of the
  103.  * fast free lists, just put it on that. Otherwise resort to the real function.
  104.  */
  105. #define    zfree(p) \
  106.         if (gahead(p)->ga_list != 0) \
  107.         { \
  108.         *(char **)(p) = ici_fflist[gahead(p)->ga_list]; \
  109.         ici_fflist[gahead(p)->ga_list] = (char *)(p); \
  110.         } \
  111.         else \
  112.         ici_free(p)
  113.  
  114. extern char    *ici_fflist[];    /* The array of free lists heads. */
  115. extern char    *ici_fftmp;    /* Temporary used in zalloc() above. */
  116. extern long    ici_old_mem;    /* Num bytes in use after last collect. */
  117. extern long    ici_new_mem;    /* Num bytes alloced since last collect. */
  118. extern void    ici_free(void *);
  119. #if    !WHOALLOC
  120. extern void    *ici_alloc(int);
  121. #else
  122. extern void    *ici_alloc(int, char *, int);
  123. #undef    zalloc
  124. #undef    zfree
  125. #define    zalloc(z)    ici_alloc((z), __FILE__, __LINE__)
  126. #define    zfree(p)    ici_free(p)
  127. #endif
  128.  
  129. #ifdef    SMALL
  130. /*
  131.  * If SMALL is defined we ignore all those macros and always use functions
  132.  * for allocation. No fast free lists are used. The address returned by
  133.  * malloc if the address of our objects. Incompatible with WHOALLOC.
  134.  * If your malloc() is even slightly slow this will significantly degrade
  135.  * performance.
  136.  */
  137. #undef    GALLOC_HEADZ
  138. #undef    gahead
  139. #undef    gadata
  140. #undef    galist
  141. #undef    zalloc
  142. #undef    zfree
  143.  
  144. #define    GALLOC_HEADZ    0
  145. #define    gahead(p)    p
  146. #define    gadata(p)    p
  147. #define    galist(z)    0
  148. #define    zalloc(z)    ici_alloc(z)
  149. #define    zfree(p)    ici_free(z)
  150. #endif /* SMALL */
  151.  
  152. #endif /* ICI_ALLOC_H */
  153.